# coding:utf-8
__author__ = "tj"

from .. import db

from werkzeug.security import generate_password_hash, check_password_hash


class Role(db.Model):
    __tablename__ = 'roles'
    id = db.Column(db.Integer, primary_key=True)
    name = db.Column(db.String(64), unique=True)
    users = db.relationship('user', backref='role', lazy='dynamic')

    def __repr__(self):
        return '<Role %r>' % self.name


class user(db.Model):
    __tablename__ = 'users'
    id = db.Column(db.Integer, primary_key=True)
    email = db.Column(db.String(64), unique=True, index=True)
    username = db.Column(db.String(64), unique=True, index=True)
    password_hash = db.Column(db.String(128))
    role_id = db.Column(db.Integer, db.ForeignKey('roles.id'))

    @property
    def password(self):
        raise AttributeError('password is not a readable attribute')

    @password.setter
    def password(self, password):
        self.password_hash = generate_password_hash(password)

    def verify_password(self, password):
        return check_password_hash(self.password_hash, password)

    #
    def __repr__(self):
        return '<User %r>' % self.username


#
# @login_manager.user_loader
# ###加载用户的回调函数接收以Unicode字符串形式表示的用户标示符
# ###如果能找到用户，这个函数必须返回用户对象，否则返回None。
# def load_user(user_id):
#     return User.query.get(int(user_id))

class run_mode(db.Model):
    __tablename__ = 'run_mode'
    id = db.Column(db.Integer, primary_key=True)
    mode = db.Column(
        db.String(64),
        unique=True,
        nullable=False)  # run_sql, run_command, sql

    # backref将在basic_sql_command表中创建个名为 run_mode 的对本表的引用,之后可以使用
    # basic_sql_command.run_mode 访问这个数据 使用哪个run_mode
    scheme_sql = db.relationship(  ## run_mode 一对多  db_recover_scheme_detail
        'db_recover_scheme_detail',
        backref='run_mode',
        lazy='dynamic')

    def __repr__(self):
        return '<run_mode %r>' % self.mode


class basic_sql_command(db.Model):
    __tablename__ = 'basic_sql_command'
    id = db.Column(db.Integer, primary_key=True)
    cmd_sql = db.Column(db.String(256), unique=True, nullable=False)

    scheme_detail = db.relationship(  #   basic_sql_command 一对多  db_recover_scheme_detail
        'db_recover_scheme_detail',
        backref='basic_sql_command',
        lazy='dynamic')


    def __repr__(self):
        return '<sql: %r>' % self.cmd_sql


class scheme_attr(db.Model):
    __tablename__ = 'scheme_attr'
    id = db.Column(db.Integer, primary_key=True)
    run_ip = db.Column(db.String(32), unique=True, nullable=False)
    db_sid = db.Column(db.String(32), nullable=True)

    run_attr = db.relationship(  ## db_recover_scheme 一对多  scheme_ip
        'db_recover_scheme_detail',
        backref="scheme_attr",
        lazy='dynamic')

    def __repr__(self):
        return '<ip: {} , sid: {}>'.format(self.scheme_ip,self.scheme_sid)


class db_recover_scheme(db.Model):
    __tablename__ = 'db_recover_scheme'
    id = db.Column(db.Integer, primary_key=True)
    desc = db.Column(db.String(64), unique=True,nullable=False)
    scheme_detail = db.relationship(  ## db_recover_scheme 一对多  db_recover_scheme_detail
        'db_recover_scheme_detail',
        backref='db_recover_scheme',
        lazy='dynamic')


    def __repr__(self):
        return '<scheme: %r>' % self.desc



class db_recover_scheme_detail(db.Model):
    __tablename__ = 'db_recover_scheme_detail'
    id = db.Column(db.Integer, primary_key=True)
    step = db.Column(db.Float, unique=True)
    # run_cmd_sql = db.Column(db.String(256), unique=True, nullable=False)
    verify_str = db.Column(db.String(64))

    basic_sql_command_id = db.Column(db.Integer, db.ForeignKey('basic_sql_command.id'))
    run_mode_id = db.Column(db.Integer, db.ForeignKey('run_mode.id'))
    scheme_id = db.Column(db.Integer, db.ForeignKey('db_recover_scheme.id'))
    attr_id = db.Column(db.Integer, db.ForeignKey('scheme_attr.id'))

    def __repr__(self):
        return '<scheme_detail: %r>' % self.step





